<template>
  <div class="bilibili-video-section">
    <!-- 区域头部 -->
    <div class="section-header">
      <h2 class="section-title">
        <span class="title-icon">{{ getSectionIcon() }}</span>
        {{ title }}
        <span v-if="showRanking" class="ranking-badge">排行榜</span>
      </h2>

      <div class="section-actions">
        <el-button
            v-if="showRefresh"
            type="text"
            size="small"
            @click="handleRefresh"
            :loading="refreshing"
        >
          <el-icon><Refresh /></el-icon>
          换一换
        </el-button>

        <el-button
            v-if="showMore"
            type="text"
            size="small"
            @click="handleViewMore"
        >
          查看更多
          <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
    </div>

    <!-- 视频网格 -->
    <div class="video-grid" :class="gridClass">
      <VideoCard
          v-for="video in displayedVideos"
          :key="video.id"
          :video="video"
          :show-ranking="showRanking"
          :type="type"
          @click="handleVideoClick(video)"
          @like="handleLike"
          @coin="handleCoin"
          @favorite="handleFavorite"
      />
    </div>

    <!-- 加载更多 -->
    <div v-if="showLoadMore && hasMore" class="load-more-section">
      <el-button
          type="primary"
          :loading="loading"
          @click="handleLoadMore"
          class="load-more-btn"
      >
        <template v-if="loading">
          <el-icon class="loading-icon"><Loading /></el-icon>
          加载中...
        </template>
        <template v-else>
          加载更多
        </template>
      </el-button>
    </div>

    <!-- 空状态 -->
    <div v-if="!videos.length && !loading" class="empty-section">
      <el-empty :description="emptyText" :image-size="100">
        <el-button type="primary" @click="$emit('refresh')">
          刷新内容
        </el-button>
      </el-empty>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading && !videos.length" class="loading-section">
      <div class="skeleton-grid">
        <div v-for="n in 6" :key="n" class="video-skeleton">
          <el-skeleton animated>
            <template #template>
              <div class="skeleton-item">
                <el-skeleton-item variant="image" class="skeleton-cover" />
                <div class="skeleton-content">
                  <el-skeleton-item variant="h3" style="width: 80%" />
                  <el-skeleton-item variant="text" style="width: 60%" />
                  <el-skeleton-item variant="text" style="width: 40%" />
                </div>
              </div>
            </template>
          </el-skeleton>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  Refresh,
  ArrowRight,
  Loading,
} from '@element-plus/icons-vue'
import VideoCard from './VideoCard.vue'

const props = defineProps({
  title: {
    type: String,
    required: true
  },
  videos: {
    type: Array,
    default: () => []
  },
  type: {
    type: String,
    default: 'recommend', // recommend, hot, new
    validator: (value) => ['recommend', 'hot', 'new'].includes(value)
  },
  loading: {
    type: Boolean,
    default: false
  },
  showRanking: {
    type: Boolean,
    default: false
  },
  showRefresh: {
    type: Boolean,
    default: true
  },
  showMore: {
    type: Boolean,
    default: true
  },
  showLoadMore: {
    type: Boolean,
    default: true
  },
  hasMore: {
    type: Boolean,
    default: true
  },
  gridType: {
    type: String,
    default: 'normal', // normal, compact, large
    validator: (value) => ['normal', 'compact', 'large'].includes(value)
  },
  pageSize: {
    type: Number,
    default: 12
  }
})

const emit = defineEmits(['load-more', 'refresh', 'video-click', 'view-more'])

const router = useRouter()

// 响应式数据
const currentPage = ref(1)
const refreshing = ref(false)

// 计算属性
const gridClass = computed(() => `grid-${props.gridType}`)

const displayedVideos = computed(() => {
  if (props.type === 'hot' && props.showRanking) {
    return props.videos.slice(0, props.pageSize)
  }
  const start = (currentPage.value - 1) * props.pageSize
  const end = start + props.pageSize
  return props.videos.slice(0, end)
})

const emptyText = computed(() => {
  const texts = {
    recommend: '暂无推荐视频',
    hot: '暂无热门视频',
    new: '暂无最新视频'
  }
  return texts[props.type] || '暂无内容'
})

// 处理方法
const handleRefresh = async () => {
  refreshing.value = true
  emit('refresh')
  // 模拟刷新延迟
  setTimeout(() => {
    refreshing.value = false
  }, 800)
}

const handleLoadMore = () => {
  if (!props.loading && props.hasMore) {
    currentPage.value++
    emit('load-more')
  }
}

const handleViewMore = () => {
  emit('view-more')
  const routes = {
    recommend: '/recommend',
    hot: '/hot',
    new: '/latest'
  }
  router.push(routes[props.type] || '/')
}

const handleVideoClick = (video) => {
  emit('video-click', video)
  router.push(`/video/${video.id}`)
}

const handleLike = (video) => {
  console.log('点赞视频:', video.title)
  // 实际项目中调用点赞API
}

const handleCoin = (video) => {
  console.log('投币视频:', video.title)
  // 实际项目中调用投币API
}

const handleFavorite = (video) => {
  console.log('收藏视频:', video.title)
  // 实际项目中调用收藏API
}

// 工具函数
const getSectionIcon = () => {
  const icons = {
    recommend: '⭐',
    hot: '🔥',
    new: '🆕'
  }
  return icons[props.type] || '📺'
}

onMounted(() => {
  console.log(`VideoSection mounted: ${props.title}`)
})
</script>

<style scoped>
.bilibili-video-section {
  margin-bottom: 32px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #00a1d6;
}

.section-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #18191c;
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-icon {
  font-size: 24px;
}

.ranking-badge {
  background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  margin-left: 8px;
}

.section-actions {
  display: flex;
  gap: 8px;
}

/* 视频网格布局 */
.video-grid {
  display: grid;
  gap: 16px;
  margin-bottom: 20px;
}

.grid-normal {
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.grid-compact {
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.grid-large {
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

/* 加载更多区域 */
.load-more-section {
  text-align: center;
  padding: 20px 0;
}

.load-more-btn {
  width: 200px;
  height: 40px;
}

.loading-icon {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 空状态 */
.empty-section {
  padding: 40px 20px;
  text-align: center;
  background: white;
  border-radius: 8px;
}

/* 加载状态 */
.loading-section {
  padding: 20px 0;
}

.skeleton-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.skeleton-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  padding: 12px;
}

.skeleton-cover {
  width: 100%;
  height: 160px;
  border-radius: 4px;
  margin-bottom: 12px;
}

.skeleton-content {
  padding: 0 8px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .grid-normal {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

@media (max-width: 768px) {
  .section-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .section-title {
    font-size: 18px;
  }

  .video-grid {
    gap: 12px;
  }

  .grid-normal,
  .grid-compact,
  .grid-large {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }

  .skeleton-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
  }
}
</style>